package work.xiaohong.gupiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import work.xiaohong.gupiao.dao.JijinMapper;
import work.xiaohong.gupiao.pojo.Jijin;
import work.xiaohong.gupiao.service.JijinService;
import work.xiaohong.gupiao.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JijinServiceImpl implements JijinService {

    @Autowired
    private JijinMapper jijinMapper;


    /**
     * 条件+分页查询Jijin
     * @param jijin 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<Jijin> findPage(Jijin jijin, int page, int size){
        // 构建查询条件
        QueryWrapper<Jijin> queryWrapper = new QueryWrapper<Jijin>(jijin);
        // 构建分页
        Page pg = new Page(page, size);
        // 分页查询
        return jijinMapper.selectPage(pg, queryWrapper);
    }

    /**
     * 分页查询Jijin
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Jijin> findPage(int page, int size){
        // 构建分页
        Page pg = new Page(page, size);
        // 分页查询
        return jijinMapper.selectPage(pg, null);
    }

    /**
     * 多条件搜索方法Jijin
     * @param jijin
     * @return
     */
    @Override
    public List<Jijin> findList(Jijin jijin){
        // 构建查询条件
        QueryWrapper<Jijin> queryWrapper = new QueryWrapper<Jijin>(jijin);
        return jijinMapper.selectList(queryWrapper);
    }

    /**
     * 根据ID删除Jijin
     * @param id
     */
    @Override
    public void delete(Integer id){
        jijinMapper.deleteById(id);
    }

    /**
     * 根据ID修改Jijin
     * @param jijin
     */
    @Override
    public void update(Jijin jijin){
        // 初始化一些不可修改数据
        jijin.setGmtModified(null);
        jijin.setGmtCreate(null);
        jijin.setVersion(null);
        jijin.setDeleted(null);
        jijinMapper.updateById(jijin);
    }

    /**
     * 增加Jijin
     * @param jijin
     */
    @Override
    public void add(Jijin jijin){
        jijinMapper.insert(jijin);
    }

    /**
     * 根据ID查询Jijin
     * @param id
     * @return
     */
    @Override
    public Jijin findById(Integer id){
        return  jijinMapper.selectById(id);
    }


    /**
     * 查询已完成订单
     * @return
     */
    @Override
    public List<Jijin> findIsOver() {
        // 构建查询条件
        QueryWrapper<Jijin> queryWrapper = new QueryWrapper<Jijin>();
        queryWrapper.in("status",1,4);
        // 根据修改时间降序
        queryWrapper.orderByDesc("dealDay");
        // 查询数据
        return jijinMapper.selectList(queryWrapper);
    }

    /**
     * 查询进行中订单
     * @return
     */
    @Override
    public List<Jijin> findIsDoing() {
        // 构建查询条件
        QueryWrapper<Jijin> queryWrapper = new QueryWrapper<Jijin>();
        queryWrapper.in("status",0,3);
        // 根据修改时间降序
        queryWrapper.orderByDesc("gmt_create");
        // 查询数据
        return jijinMapper.selectList(queryWrapper);
    }

    /**
     * 查询所有未满可赎天数的Jijin
     * @return
     */
    @Override
    public List<Jijin> findIsNotFree() {
        // 构建查询条件
        QueryWrapper<Jijin> queryWrapper = new QueryWrapper<Jijin>();
        queryWrapper.notIn("freeDay",0);
        // 查询数据
        return jijinMapper.selectList(queryWrapper);
    }

    /**
     * 查询全部满七天Jijin的份额
     * @return
     */
    @Override
    public Float findByDay(String code) {
        Jijin jijin = new Jijin();
        jijin.setCode(code);
        jijin.setStatus(1);
        // 构建查询条件
        QueryWrapper<Jijin> queryWrapper = new QueryWrapper<Jijin>(jijin);
        queryWrapper.notIn("buyDay", TimeUtil.getDay(0),
                TimeUtil.getDay(-1),
                TimeUtil.getDay(-2),
                TimeUtil.getDay(-3),
                TimeUtil.getDay(-4),
                TimeUtil.getDay(-5),
                TimeUtil.getDay(-6),
                TimeUtil.getDay(-7));

        // 查询所有可赎数总额
        List<Jijin> jijins = jijinMapper.selectList(queryWrapper);
        Float sum = 0F;
        for (Jijin jj1 : jijins) {
            sum += Float.parseFloat(jj1.getBuyNumber());
        }

        // 查询已经用去的可赎额度
        // 构建查询条件
        QueryWrapper<Jijin> queryWrapper1 = new QueryWrapper<Jijin>();
        queryWrapper1.in("status",0,3);
        queryWrapper1.in("buyDay",TimeUtil.getDay(0),
                TimeUtil.getDay(-1),
                TimeUtil.getDay(-2),
                TimeUtil.getDay(-3),
                TimeUtil.getDay(-4),
                TimeUtil.getDay(-5),
                TimeUtil.getDay(-6),
                TimeUtil.getDay(-7));

        List<Jijin> jijins1 = jijinMapper.selectList(queryWrapper1);
        for (Jijin jj2 : jijins1) {
            sum -= Float.parseFloat(jj2.getBuyNumber());
        }


        // 查询正在进行中的可赎额度
        Jijin jijin2 = new Jijin();
        jijin2.setCode(code);
        jijin2.setStatus(4);
        // 构建查询条件
        QueryWrapper<Jijin> queryWrapper2 = new QueryWrapper<Jijin>(jijin2);
        queryWrapper2.in("buyDay",TimeUtil.getDay(0),
                TimeUtil.getDay(-1),
                TimeUtil.getDay(-2),
                TimeUtil.getDay(-3),
                TimeUtil.getDay(-4),
                TimeUtil.getDay(-5),
                TimeUtil.getDay(-6),
                TimeUtil.getDay(-7));

        List<Jijin> jijins2 = jijinMapper.selectList(queryWrapper2);
        for (Jijin jj3 : jijins2) {
            sum -= Float.parseFloat(jj3.getBuyNumber());
        }


        return sum;
    }


    /**
     * 查询全部Jijin数据
     * @return
     */
    @Override
    public List<Jijin> findAll() {
        return jijinMapper.selectList(null);
    }


    public static void main(String[] args) {
        System.out.println(TimeUtil.getDay(-1));
    }

}
